/**
 * Copyright (c) 2008, Keisuke Inoue, Syracuse Unviersity
 * All rights reserved.
 *
 */
package edu.syr.iis.web.model;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import edu.syr.iis.web.util.HibernateUtil;

/**
 * @author kinoue
 *
 */
public class DialogueActDimensionManager
{
	/**
	 * Returns list of all DialogueActDimensions of the specified DialogueActScheme sorted by dialogueActDimensionId
	 */
	public List<DialogueActDimension> getDialogueActDimensions(DialogueActScheme dialogueActScheme)
	{
		List<DialogueActDimension> list = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			String query = "FROM DialogueActDimension WHERE dialogueActSchemeId = ?";
			list = session.createQuery(query).setInteger(0, dialogueActScheme.getDialogueActSchemeId()).list();

			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return list;
	}

	/**
	 * Returns list of all DialogueActDimension  sorted by DialogueActDimensionId
	 */
	public List<DialogueActDimension> getDialogueActDimensions()
	{	
		List<DialogueActDimension> list = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			Criteria criteria = session
				.createCriteria(DialogueActDimension.class)
				.addOrder(Order.asc("dialogueActDimensionId"));
			list = criteria.list();
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return list;
	}

	public DialogueActDimension getDialogueActDimensionBySchemeAndDimension(String scheme, String dimension)
	{
		DialogueActDimension dialogueActDimension = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			dialogueActDimension = (DialogueActDimension) session.createQuery(
					"FROM DialogueActDimension " +
			" WHERE dialogueActScheme.scheme = ? and dimension = ?")
			.setString(0, scheme).setString(1, dimension).uniqueResult();
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return dialogueActDimension;
	}

	public DialogueActDimension getDialogueActDimension(String dimension)
	{
		DialogueActDimension dialogueActDimension = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			Criteria criteria = session.createCriteria(DialogueActDimension.class)
			.add(Restrictions.eq("dimension", dimension));
			dialogueActDimension = (DialogueActDimension) criteria.uniqueResult();			
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return dialogueActDimension;
	}
	
	
	public DialogueActDimension getDialogueActDimension(int dialogueActDimensionId)
	{
		DialogueActDimension dialogueActDimension = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();

		try
		{
			dialogueActDimension = (DialogueActDimension) session.createQuery(
					"FROM DialogueActDimension " +
			" WHERE dialogueActDimensionId = ?").setInteger(0, dialogueActDimensionId).uniqueResult();
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
		return dialogueActDimension;
	}

	/**
	 * Saves a DialogueActDimension object.
	 */
	public void saveDialogueActDimension(DialogueActDimension dialogueActDimension)
	{
		Session session = HibernateUtil.getSessionFactory()
		.getCurrentSession();
		session.beginTransaction();
		try
		{
			session.saveOrUpdate(dialogueActDimension);
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
	}

	/**
	 * Deletes DialogueActDimension record with matching dialogueActDimensionId 
	 */
	public void deleteDialogueActDimension(int dialogueActDimensionId)
	{
		Session session = 
			HibernateUtil.getSessionFactory().getCurrentSession();
		session.beginTransaction();	
		try
		{
			session.delete(session.load(DialogueActDimension.class,new Integer(dialogueActDimensionId)));         	    			
			session.flush();
			session.getTransaction().commit();
		}
		catch (HibernateException e)
		{
			session.getTransaction().rollback();
			throw e;
		}
	}


}
